{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 测试BatchNorm2d用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[ 1.,  1.],\n",
      "         [ 1.,  2.]],\n",
      "\n",
      "        [[-1.,  1.],\n",
      "         [ 0.,  1.]]])\n",
      "tensor([[[ 0., -1.],\n",
      "         [ 2.,  2.]],\n",
      "\n",
      "        [[ 0., -1.],\n",
      "         [ 3.,  1.]]])\n",
      "torch.Size([2, 2, 2])\n"
     ]
    }
   ],
   "source": [
    "x1 = torch.tensor([[[1, 1], [1, 2]], [[-1, 1], [0, 1]]], dtype=torch.float32)\n",
    "x2 = torch.tensor([[[0, -1], [2, 2]], [[0, -1], [3, 1]]], dtype=torch.float32)\n",
    "print(f'{x1}\\n{x2}')\n",
    "print(x1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[[ 1.,  1.],\n",
      "          [ 1.,  2.]],\n",
      "\n",
      "         [[-1.,  1.],\n",
      "          [ 0.,  1.]]],\n",
      "\n",
      "\n",
      "        [[[ 0., -1.],\n",
      "          [ 2.,  2.]],\n",
      "\n",
      "         [[ 0., -1.],\n",
      "          [ 3.,  1.]]]])\n",
      "torch.Size([2, 2, 2, 2])\n"
     ]
    }
   ],
   "source": [
    "x = torch.cat((x1.unsqueeze(0), x2.unsqueeze(0)), dim=0)\n",
    "print(f'{x}\\n{x.shape}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 0.0000,  0.0000],\n",
       "          [ 0.0000,  1.0000]],\n",
       "\n",
       "         [[-1.2247,  0.4082],\n",
       "          [-0.4082,  0.4082]]],\n",
       "\n",
       "\n",
       "        [[[-1.0000, -2.0000],\n",
       "          [ 1.0000,  1.0000]],\n",
       "\n",
       "         [[-0.4082, -1.2247],\n",
       "          [ 2.0412,  0.4082]]]], grad_fn=<NativeBatchNormBackward0>)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = nn.BatchNorm2d(2)\n",
    "f(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 交叉熵损失"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = tensor([[ 1.8750,  0.5672, -0.9249, -0.3654,  1.0278],\n",
      "        [ 0.8115,  1.2725,  0.2274, -0.3150,  0.5493],\n",
      "        [ 0.0243, -0.3601,  0.1398, -0.6998,  0.8926]])\n",
      "tensor(1.9450)\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(3, 5)\n",
    "print(f'x = {x}')\n",
    "label = torch.tensor([1, 0, 3])\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "loss = loss_fn(x, label)\n",
    "print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p1 = tensor([[0.5358, 0.1449, 0.0326, 0.0570, 0.2297],\n",
      "        [0.2360, 0.3743, 0.1316, 0.0765, 0.1816],\n",
      "        [0.1763, 0.1201, 0.1979, 0.0855, 0.4202]])\n",
      "sum = tensor([[1.0000],\n",
      "        [1.0000],\n",
      "        [1.0000]])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_94831/3089901900.py:2: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.\n",
      "  p1 = softmax(x)\n"
     ]
    }
   ],
   "source": [
    "softmax = nn.Softmax()\n",
    "p1 = softmax(x)\n",
    "print(f'p1 = {p1}')\n",
    "print(f'sum = {torch.sum(p1, dim=1, keepdim=True)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成vgg16网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchvision import models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "vgg16 = models.vgg16()\n",
    "torch.save(vgg16, \"../data_set/vgg16.pth\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "d2l",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
